package demo.icanci.reflect;

import java.lang.reflect.Field;

/**
 * @author icanci
 * @date Created in 2021/02/04 23:59
 * @version 1.0
 */
public class FieldController {
    public static void main(String[] args) throws Exception {
        Class<?> clazz = Class.forName("demo.icanci.reflect.ReflectTarget");
        System.out.println("获取所有的共有字段");
        Field[] fields = clazz.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
        System.out.println("获取所有的字段");
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }
        System.out.println("获取单个字段");
        Field name = clazz.getField("name");
        System.out.println(name);
        System.out.println("获取私有字段");
        Field pwd = clazz.getDeclaredField("pwd");
        System.out.println(pwd);
        ReflectTarget o = (ReflectTarget) clazz.getConstructor().newInstance();
        name.set(o, "hekko");
        pwd.setAccessible(true);
        pwd.set(o,"23423423");
        System.out.println(o.name);
        System.out.println(o);
    }
}
